package com.shijie.controller.erp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.ErpUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.domain.entity.UserOrders;
import com.shijie.service.UserOrdersService;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/erp/orders")
public class UserOrdersController {

    @Resource
	private UserOrdersService userOrdersService;

    @Resource
    private ErpUserService erpUserService;

    @RequiresPermissions(value={"order:list","signorder:list"})
    @GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(defaultValue = "0") Integer entityType, @RequestParam(required=false) Integer status, @RequestParam(required=false) Integer buyerId, @RequestParam(required=false) String buyerName) {
        PageHelper.startPage(page, pageSize);
        UserOrders qCondition = new UserOrders();
        qCondition.setEntityType(entityType);
        if(status != null){
        	qCondition.setStatus(status);	
        }
        if(buyerId != null){
        	qCondition.setBuyerId(buyerId);	
        }
        if(buyerName != null){
        	qCondition.setBuyerName(buyerName);
        }
        qCondition.setBrandId(erpUserService.getAdmin().getBrandId());
        List<UserOrders> list = userOrdersService.selectOrdersForUser(qCondition);
        PageInfo<UserOrders> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
    }
    
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        return ResultGenerator.success(userOrdersService.selectById(id));
	}
    
    @PutMapping("/{id}/{status}")
    public Result updateOrderStatus(@PathVariable("id") Integer id,@PathVariable("status") Integer status) {
    	userOrdersService.updateOrderStatus(id, status);
        return ResultGenerator.success();
	}
    
    @RequiresPermissions(value={"order:delevery"})
    @PostMapping
    public Result updateOrderStatus(@RequestBody UserOrders order) {
    	userOrdersService.update(order);
        return ResultGenerator.success();
	}
}

